﻿package extend.js
{
    import com.gengine.core.*;
    import com.gengine.core.frame.*;
    import com.gengine.global.*;
    import com.gengine.manager.*;
    import extend.php.*;
    import flash.events.*;
    import flash.external.*;
    import flash.system.*;
    import mortal.common.global.*;
    import mortal.game.scene3D.map3D.util.*;
    import mortal.game.utils.*;

    public class JSASSender extends Object
    {
        private var type:int = 0;
        private static var _instance:JSASSender;

        public function JSASSender()
        {
            if (_instance != null)
            {
                throw new Error("JSASSender单例");
            }
            Security.allowDomain("*");
            return;
        }// end function

        private function jsClose() : void
        {
            if (PHPSender.isCreateRole)
            {
                PHPSender.sendToURLByPHP(SenderType.FlashClose);
            }
            PHPSender.setToPHPMapFrame(MapFileUtil.mapID, FrameUtil.driveInfo, 0);
            return;
        }// end function

        private function cacheClear() : void
        {
            CacheManager.instance.clear();
            return;
        }// end function

        private function init() : void
        {
            if (ExternalInterface.available)
            {
                ExternalInterface.marshallExceptions = true;
                try
                {
                    ExternalInterface.addCallback("cacheClear", this.cacheClear);
                    ExternalInterface.addCallback("jsClose", this.jsClose);
                }
                catch (e)
                {
                }
            }
            return;
        }// end function

        public function addCloseHandler() : void
        {
            var _loc_1:String = null;
            if (ExternalInterface.available)
            {
                try
                {
                    _loc_1 = ActiveUtil.getUnfinishStr();
                    if (ParamsConst.instance.client != 1)
                    {
                        BrowerManager.instance.confirmClose(_loc_1);
                    }
                }
                catch (e)
                {
                }
            }
            return;
        }// end function

        public function start() : void
        {
            Global.stage.addEventListener(Event.ENTER_FRAME, this.onEnterFrameHandler);
            return;
        }// end function

        private function onEnterFrameHandler(event:Event) : void
        {
            if (!ExternalInterface.available)
            {
                return;
            }
            Global.stage.removeEventListener(Event.ENTER_FRAME, this.onEnterFrameHandler);
            this.init();
            return;
        }// end function

        private function callJsSetInterval() : void
        {
            if (!ExternalInterface.available)
            {
                return;
            }
            ExternalInterface.call("asCallJs");
            return;
        }// end function

        public function maxMapResize() : void
        {
            if (!ExternalInterface.available)
            {
                return;
            }
            ExternalInterface.call("callMapResize", 0);
            return;
        }// end function

        public function callMapResize(param1:int) : void
        {
            if (!ExternalInterface.available)
            {
                return;
            }
            if (this.type == 1)
            {
                this.type = 0;
            }
            else
            {
                this.type = 1;
            }
            ExternalInterface.call("callMapResize", this.type);
            return;
        }// end function

        public function jsCall() : void
        {
            FrameManager.flashFrame.dispatchEvent();
            return;
        }// end function

        public static function get instance() : JSASSender
        {
            if (_instance == null)
            {
                _instance = new JSASSender;
            }
            return _instance;
        }// end function

    }
}
